package com.spring.annotation.config;

import com.spring.annotation.bean.Person;
import com.spring.annotation.condition.DemoCondition;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

//配置类==配置文件
@Configuration //告诉Spring这是一个配置类
/**
 * Title：包扫描和扫描规则
 *
 * 包扫描：
 * @ComponentScan value:指定要扫描的包——@ComponentScan(value = "com.spring.annotation")
 *
 * 扫描规则：
 * excludeFilters = Filter[],指定扫描的时候按照什么规则排除那些组件
 *      // 包扫描排除注解类型为Controller.class和Service.class的class
 *       @ComponentScan(value = "com.spring.annotation",excludeFilters = {
 *                 @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
 *         })
 * includeFilters = Filter[],指定扫描的时候按照什么规则包含那些组件  注意：需要禁用默认的过滤规则：useDefaultFilters = false
 *      // 包扫描扫描注解类型包含Service的class
 *      @ComponentScan(value = "com.spring.annotation",includeFilters = {
 *                 @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class})
 *         },useDefaultFilters = false)
 *
 *  其他扫描的类型：
 *  FilterType.ANNOTATION     是按照注解的方式 过滤**（默认的方式）**
 *  FilterType.ASPECTJ        按照ASPECTJ 表达式(一般不会使用)
 *  FilterType.REGEX          按照正则表达式
 *  FilterType.ASSIGNABLE_TYPE 按照指定的类型
 *  FilterType.CUSTOM          按照自定义规则
 *
 *  命名多个扫描规则
 *  1、@ComponentScan 因为 @Repeatable 可以命名多个扫描规则
 *  2、也可以使用@ComponentScans命名多个@ComponentScan
 *      @ComponentScans(value = {
 *         @ComponentScan(value = "com.spring.annotation",excludeFilters = {
 *                 @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
 *         }), @ComponentScan(value = "com.spring.annotation",includeFilters = {
 *                 @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class})
 *         },useDefaultFilters = false)
 *      })
 *
 *  自定义的扫描规则 Filter
 *      自定义包扫描过滤规则——public class MyFilterType implements TypeFilter{}
 */
@ComponentScans(value = {
        @ComponentScan(value = "com.spring.annotation",excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
        }), @ComponentScan(value = "com.spring.annotation",includeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class})
        },useDefaultFilters = false)
})
// 条件判断注解 可以放到类上和方法上
@Conditional({DemoCondition.class})
public class BeanConfig {
    @Bean(name = "PersonOfConfig") //给容器中注册一个Bean类型为返回值的类型，id默认是用方法名作为id
    public Person person(){
        System.out.println("bean加入ioc容器");
        return new Person("lisi",20);
    }

}
